/*
用例使用的开发的，http://wiki.sugon.com/pages/viewpage.action?pageId=78093051
*/
#include <hip/hip_runtime.h>
#include <iostream>
#include <unistd.h>
#include <vector>

#define BUF_WIDTH 1024
#define BUF_HEIGHT 1024
#define BUF_SIZE BUF_WIDTH * BUF_HEIGHT
#define TEST_TIME 10

int memMalloc(std::vector<void*>& ptrVector)
{
    hipError_t ret = hipSuccess;
    for (size_t i = 0; i < TEST_TIME; i++)
    {
        void* ptr2 = nullptr;
        ret = hipMalloc((void**)&ptr2, BUF_SIZE * 5);
        if (ret != hipSuccess) { return -2; }
        ptrVector.push_back(ptr2);
        usleep(100000);
    }

    return 0;
}

int memFree(std::vector<void*>& ptrVector)
{
    hipError_t ret = hipSuccess;
    for (size_t i = 0; i < TEST_TIME; i++)
    {
        void* ptr2 = ptrVector.back();
        hipFree(ptr2);
        ptrVector.pop_back();
    }

    return 0;
}

int main()
{
    hipDeviceProp_t devProp;
    hipGetDeviceProperties(&devProp, 0);
    std::cout << "dcu" << 0 << ": " << devProp.name <<std::endl;

    std::vector<void*> ptrVector;
    memMalloc(ptrVector);
    memMalloc(ptrVector);
    memFree(ptrVector);
    memMalloc(ptrVector);
    memMalloc(ptrVector);
    memFree(ptrVector);
    memMalloc(ptrVector);
    memFree(ptrVector);
    memMalloc(ptrVector);
    memFree(ptrVector);

    return 0;
}
